import { routesConfig } from "../config/RouteConfig"
import { userStore } from "../stores/UserStore"
import { LocalStorageService } from "./storage"
import GlobalConfig from "../config/GlobalConfig";
import { showError } from "../components/Message";
import ChartStore from "../stores/ChartStore";
import StatisticStore from "../stores/StatisticStore";
import { Ajax } from "./http";
import { StaticticService, StatisticResponse } from "../services/StatisticService";



export function registerGlobalFuncs() {
  (window as any).stockSearch = async (symbol: string) => {
    ChartStore.initState();
    
    var data: any = await new Ajax('/prices', {
      symbol: symbol,
    }, true).get();
    var statisticData: StatisticResponse;
    try {
      statisticData = await new StaticticService(symbol).send();
    } catch (e) {
      return;
    }
    ChartStore.pending = false;
    ChartStore.stockData = data;
    StatisticStore.paintChart(statisticData);
  }
}



export function scrollAnimation(currentY: number, targetY: number) {
  // 获取当前位置方法
  // const currentY = document.documentElement.scrollTop || document.body.scrollTop

  // 计算需要移动的距离
  let needScrollTop = targetY - currentY
  let _currentY = currentY
  setTimeout(() => {
    // 一次调用滑动帧数，每次调用会不一样
    const dist = Math.ceil(needScrollTop / 10)
    _currentY += dist
    window.scrollTo(_currentY, currentY)
    // 如果移动幅度小于十个像素，直接移动，否则递归调用，实现动画效果
    if (needScrollTop > 10 || needScrollTop < -10) {
      scrollAnimation(_currentY, targetY)
    } else {
      window.scrollTo(_currentY, targetY)
    }
  }, 1)
}

export function goto(link: string) {
  let targetRouterConfig: any;
  let isLogin = LocalStorageService.get("token");
  routesConfig.forEach((item) => {
    if (item.routes && item.routes.length > 0) {
      targetRouterConfig = item.routes.find(
        (r: any) => {
          return r.path.replace(/\s*/g, "") === link
        });
    } else if (item.path.replace(/\s*/g, "") === link) {
      targetRouterConfig = item;
    }
  })
  if (!targetRouterConfig) {
    return;
  }
  if (targetRouterConfig.auth || targetRouterConfig.rule) {
    if (isLogin) {
      if (targetRouterConfig.rule) {
        let rules = userStore.userInfo?.rules || [];
        if (rules?.indexOf(targetRouterConfig.rule) > -1) {
          GlobalConfig.history.push(link);
        } else {
          showError('订阅套餐后方可进行访问！')
          return;
        }
      }
      window.scrollTo(0, 0);
      GlobalConfig.history.push(link);
    } else {
      userStore.loginModalVisible = true;
    }
  } else {
    window.scrollTo(0, 0);
    GlobalConfig.history.push(link);
  }
}

export function timestamp(): string {
  var time = new Date();
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  function add0(m: number) {
    return m < 10 ? '0' + m : m;
  }
  return "" + y + add0(m) + add0(d) + add0(h) + add0(mm) + add0(s);
}




export function numFormatter(value: number, fixed?: number): string {
  var result: string;
  var T: number = value / 10;
  var K: number = value / 1000;
  var M: number = value / 1000000;
  var B: number = value / 1000000000;
  if ((Math.abs(T)) > 1000) {
    if ((Math.abs(K)) > 1000) {
      if ((Math.abs(M)) > 1000) {
        let _v = fixed ? B.toFixed(fixed) : B.toString()
        result = _v + (fixed ? ' ' : '') + 'B';
      } else {
        let _v = fixed ? M.toFixed(fixed) : M.toString()
        result = _v + (fixed ? ' ' : '') + 'M'
      }
    } else {
      let _v = fixed ? K.toFixed(fixed) : K.toString()
      result = _v + (fixed ? ' ' : '') + 'K'
    }
  } else {
    result = fixed ? value.toFixed(fixed) : value.toString();
  }
  return result;
}